﻿@{
    var formSearchCtrlHtml = ViewBag.FormSearchCtrlHtml as List<Dictionary<string, string>>;
    int totalCount = formSearchCtrlHtml.Count();
    int index = (int)Math.Ceiling(totalCount / 3m);//三列
}
<div id="tb">
    <table cellpadding="0" cellspacing="0" style="width: 100%">
        <tr>
            <td>
                @Html.Raw(ViewBag.ButtonHtml)
            </td>
            <td style="text-align:right">
                @*totalCount == 0 详细查询与快速搜索不同存*@
                @if (totalCount == 0)
                {
                    if (!string.IsNullOrEmpty(ViewBag.QuickSearchColumTitle))
                    {
                        <input class="easyui-searchbox" data-options="prompt:'输入@{@ViewBag.QuickSearchColumTitle}',searcher:doSearch" style="width:200px">
                    }

                }
                @*详细查询的 查询按钮及清空按钮*@
                else
                {
                    <a href="#" class="easyui-linkbutton" iconcls="iconfont iconfont-sousuo" plain="true" onclick="formSearch()">查询</a>
                    <a href="#" class="easyui-linkbutton" iconcls="iconfont iconfont-chexiao" plain="true" onclick="formSearchClear()">清空</a>
                }
            </td>
        </tr>
    </table>

    @* 详细查询 *@
    @if (totalCount > 0)
    {
        <form id="searchForm">
            <table class="groupTable" style="margin-top:3px;">
                <tbody>
                    @for (int i = 0; i < index; i++)
                    {
                        <tr>
                            @if (i * 3 < totalCount)
                            {
                                var searchCol = formSearchCtrlHtml[i * 3];
                                <td style="width:10%">@searchCol["title"]</td>
                                <td class="relationSymbol" style="display:none">@searchCol["relationSymbol"]</td>
                                <td class="searchCtrlTypeSymbol" style="display:none">@searchCol["searchCtrlTypeSymbol"]</td>
                                <td style="width:23%">@Html.Raw(searchCol["ctrl"])</td>
                            }
                            else
                            {
                                <td style="width:10%"></td>
                                <td style="width:23%"></td>
                            }
                            @if (i * 3 + 1 < totalCount)
                            {
                                var searchCol = formSearchCtrlHtml[i * 3 + 1];
                                <td style="width:10%">@searchCol["title"]</td>
                                <td class="relationSymbol" style="display:none">@searchCol["relationSymbol"]</td>
                                <td class="searchCtrlTypeSymbol" style="display:none">@searchCol["searchCtrlTypeSymbol"]</td>
                                <td style="width:23%">@Html.Raw(searchCol["ctrl"])</td>
                            }
                            else
                            {
                                <td style="width:10%"></td>
                                <td style="width:23%"></td>
                            }
                            @if (i * 3 + 2 < totalCount)
                            {
                                var searchCol = formSearchCtrlHtml[i * 3 + 2];
                                <td style="width:10%">@searchCol["title"]</td>
                                <td class="relationSymbol" style="display:none">@searchCol["relationSymbol"]</td>
                                <td class="searchCtrlTypeSymbol" style="display:none">@searchCol["searchCtrlTypeSymbol"]</td>
                                <td style="width:24%">@Html.Raw(searchCol["ctrl"])</td>
                            }
                            else
                            {
                                <td style="width:10%"></td>
                                <td style="width:24%"></td>
                            }
                        </tr>
                    }
                </tbody>
            </table>
        </form>
    }
</div>
@Html.Raw(ViewBag.ListHtml)
<!--导出Excel——模拟异步ajax提交表单 -->
<form id='exportExcelForm' style='display:none;' action='/AutoUI/ConfigUI/NPOI/ExportExcel' method='post' target='exportExcelIFrame'>
    <input type='hidden' name='gridUrl' />
    <input type='hidden' name='referUrl' />
    <input type='hidden' name='pageNumber' />
    <input type='hidden' name='pageSize' />
    <input type='hidden' name='queryParams' />
    <input type='hidden' name='title' />
    <input type='hidden' name='sortName' />
    <input type='hidden' name='sortOrder' />
    <input type='hidden' name='columns' />
</form>
<iframe id='exportExcelIFrame' name='exportExcelIFrame' style='display:none;'></iframe>

<script>
    var code = queryString('UICode');
    var title = '@ViewBag.Title';
    @Html.Raw(ViewBag.Script)
</script>
<script>
    $('#mf_grid').datagrid({ url: 'GetList', queryParams: { UICode: code } });
    function thisDelRow(e) {
        var url = '/AutoUI/ConfigUI/List/Delete';
        delRow({ url: url, params: { UICode: code } });
    }

    function openWindowWithUrl(url, width, height) {
        url = url.replace('{UICode}', code);

        var rows = $('#mf_grid').datagrid('getSelections');
        if (rows.length != 0) {
            for (var key in rows[0]) {
                url = url.replace('{' + key + '}', rows[0][key]);
            }
        }

        openWindow(url, {
            width: width,
            height: height,
            onDestroy: function (data) {
                if (data) {
                    $("#mf_grid").datagrid('reload');
                    $('#mf_grid').datagrid('clearSelections');
                }
            }
        });
    }

    function doSearch(value, name) {
        $('#mf_grid').datagrid('load', {
            UICode: code,
            QuickSearchValue: value
        });
    }

    function exportList() {
        exportExcel(title || '导出的数据');
    }

    function formSearch() {
        $('#mf_grid').datagrid('load', {
            UICode: code,
            QueryItems: getFormSearchData()
        });
    }
    
    function formSearchClear() {
        $('#searchForm').form('clear');
    }
</script>
